package cn.exrick.xboot.modules.assessment.controller;

import cn.exrick.xboot.base.XbootBaseController;
import cn.exrick.xboot.common.utils.CustomPage;
import cn.exrick.xboot.common.utils.CustomPageUtil;
import cn.exrick.xboot.common.utils.PageUtil;
import cn.exrick.xboot.common.utils.ResultUtil;
import cn.exrick.xboot.common.vo.PageVo;
import cn.exrick.xboot.common.vo.Result;
import cn.exrick.xboot.common.vo.SearchVo;
import cn.exrick.xboot.modules.assessment.entity.HkAssessmentIndex;
import cn.exrick.xboot.modules.assessment.entity.HkFormIndex;
import cn.exrick.xboot.modules.assessment.service.HkAssessmentIndexService;
import cn.exrick.xboot.modules.assessment.service.HkFormIndexService;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author yck
 */
@Slf4j
@RestController
@Api(description = "表单项管理接口")
@RequestMapping("/xboot/hkFormIndex")
@Transactional
public class HkFormIndexController extends XbootBaseController<HkFormIndex, String> {

    @Autowired
    private HkFormIndexService hkFormIndexService;
    @PersistenceContext
    private EntityManager entityManager;
    @Autowired
    private HkAssessmentIndexService hkAssessmentIndexService;

    @Override
    public HkFormIndexService getService() {
        return hkFormIndexService;
    }

    @RequestMapping(value = "/getByCondition", method = RequestMethod.GET)
    @ApiOperation(value = "多条件分页获取")
    public Result<CustomPage> getByCondition(HkFormIndex hkFormIndex,
                                                            SearchVo searchVo,
                                                            PageVo pageVo){

        Page<HkFormIndex> page = hkFormIndexService.findByCondition(hkFormIndex, searchVo, PageUtil.initPage(pageVo));
        page.getContent().stream().forEach(index->{
            if(index.getAssessmentIndexId()!=null){
                HkAssessmentIndex hkAssessmentIndex = hkAssessmentIndexService.get(index.getAssessmentIndexId());
                if(hkAssessmentIndex!=null){
                    index.setIndexName(hkAssessmentIndex.getIndexName());
                }
            }
        });

        CustomPage customPage = CustomPageUtil.getCustomPage(page);

        return new ResultUtil<CustomPage>().setData(customPage);
    }
    @RequestMapping(value = "/hkFormIndexAll", method = RequestMethod.GET)
    @ApiOperation(value = "获取所有数据")
    public Result formIndexAll(HkFormIndex hkFormIndex){
        List<HkFormIndex> all = hkFormIndexService.getAllList(hkFormIndex);
        Gson son=new Gson();
        Map map=new HashMap();
        entityManager.clear();
        all.stream().forEach(index->{
            String formIndexJson = index.getFormIndexJson();
            index.setFormIndexJson("");
            Object o = son.fromJson(formIndexJson, Object.class);
            index.setFormIndexJsonObject(o);

        });
        return new ResultUtil<List<HkFormIndex>>().setData(all);
    }
    @RequestMapping(value = "/relationIndex",method = RequestMethod.PUT)
    @ResponseBody
    @ApiOperation(value = "关联指标")
    public Result relationIndex(String id,String assessmentIndexId){
        HkFormIndex hkFormIndex = hkFormIndexService.get(id);
        hkFormIndex.setAssessmentIndexId(assessmentIndexId);
        hkFormIndexService.update(hkFormIndex);
        return ResultUtil.success("关联成功！");
    }
    /*
     * 设置为自定义减项
     * */
    @RequestMapping(value = "/setCustomDeleteFormIndex",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "设置为自定义减表单项")
    public Result setCustomDeleteFormIndex(String id) throws Exception {
        hkFormIndexService.setCustomDeleteFormIndex(id);
        return ResultUtil.success("设置成功！");
    }
    /*
     * 设置为自定义减项
     * */
    @RequestMapping(value = "/setCustomAddFormIndex",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "设置为自定义减表单项")
    public Result setCustomAddFormIndex(String id) throws Exception {
        hkFormIndexService.setCustomAddFormIndex(id);
        return ResultUtil.success("设置成功！");
    }
    @RequestMapping(value = "/updateFormIndex",method = RequestMethod.PUT)
    @ResponseBody
    @ApiOperation(value = "表单项修改")
    public Result updateFormIndex(String id,String formIndexName,String formIndexJson) throws Exception {
        HkFormIndex hkFormIndex = hkFormIndexService.get(id);
        hkFormIndex.setFormIndexName(formIndexName);
        hkFormIndex.setFormIndexJson(formIndexJson);
        hkFormIndex.setUpdateTime(new Date());
        hkFormIndexService.update(hkFormIndex);
        return ResultUtil.success("修改成功！");
    }
}
